home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP06.ZIP / CHAP06 / POLYLINE / IDATAOBJ.CPP < prev    next >
C/C++ Source or Header  |  1993-04-21  |  12KB  |  485 lines

  1. /*
  2.  * IDATAOBJ.CPP
  3.  * Polyline Component Object Chapter 6
  4.  *
  5.  * Template implementation of the IDataObject interface.
  6.  *
  7.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Software Design Engineer
  10.  * Microsoft Systems Developer Relations
  11.  *
  12.  * Internet  :  kraigb@microsoft.com
  13.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  14.  */
  15.  
  16.  
  17. #include "polyline.h"
  18.  
  19.  
  20. /*
  21.  * CImpIDataObject::CImpIDataObject
  22.  * CImpIDataObject::~CImpIDataObject
  23.  *
  24.  * Parameters (Constructor):
  25.  *  pObj            LPVOID of the object we're in.
  26.  *  punkOuter       LPUNKNOWN to which we delegate.
  27.  */
  28.  
  29. CImpIDataObject::CImpIDataObject(LPVOID pObj, LPUNKNOWN punkOuter)
  30.     {
  31.     m_cRef=0;
  32.     m_pObj=pObj;
  33.     m_punkOuter=punkOuter;
  34.     return;
  35.     }
  36.  
  37. CImpIDataObject::~CImpIDataObject(void)
  38.     {
  39.     return;
  40.     }
  41.  
  42.  
  43.  
  44.  
  45. /*
  46.  * CImpIDataObject::QueryInterface
  47.  * CImpIDataObject::AddRef
  48.  * CImpIDataObject::Release
  49.  *
  50.  * Purpose:
  51.  *  IUnknown members for CImpIDataObject object.
  52.  */
  53.  
  54. STDMETHODIMP CImpIDataObject::QueryInterface(REFIID riid, LPVOID FAR *ppv)
  55.     {
  56.     return m_punkOuter->QueryInterface(riid, ppv);
  57.     }
  58.  
  59.  
  60. STDMETHODIMP_(ULONG) CImpIDataObject::AddRef(void)
  61.     {
  62.     ++m_cRef;
  63.     return m_punkOuter->AddRef();
  64.     }
  65.  
  66. STDMETHODIMP_(ULONG) CImpIDataObject::Release(void)
  67.     {
  68.     --m_cRef;
  69.     return m_punkOuter->Release();
  70.     }
  71.  
  72.  
  73.  
  74.  
  75.  
  76. /*
  77.  * CImpIDataObject::GetData
  78.  *
  79.  * Purpose:
  80.  *  Retrieves data described by a specific FormatEtc into a StgMedium
  81.  *  allocated by this function.  Used like GetClipboardData.
  82.  *
  83.  * Parameters:
  84.  *  pFE             LPFORMATETC describing the desired data.
  85.  *  pSTM            LPSTGMEDIUM in which to return the data.
  86.  *
  87.  * Return Value:
  88.  *  HRESULT         NOERROR on success, error code otherwise.
  89.  */
  90.  
  91. STDMETHODIMP CImpIDataObject::GetData(LPFORMATETC pFE, LPSTGMEDIUM pSTM)
  92.     {
  93.     LPCPolyline     pObj=(LPCPolyline)m_pObj;
  94.     UINT            cf=pFE->cfFormat;
  95.  
  96.     //Check the aspects we support.
  97.     if (!(DVASPECT_CONTENT & pFE->dwAspect))
  98.         return ResultFromScode(DATA_E_FORMATETC);
  99.  
  100.     //Go render the appropriate data for the format.
  101.     switch (cf)
  102.         {
  103.         case CF_METAFILEPICT:
  104.             pSTM->tymed=TYMED_MFPICT;
  105.             return pObj->RenderMetafilePict(&pSTM->hGlobal);
  106.  
  107.         case CF_BITMAP:
  108.             pSTM->tymed=TYMED_GDI;
  109.             return pObj->RenderBitmap((HBITMAP FAR *)&pSTM->hGlobal);
  110.  
  111.         default:
  112.             if (cf==pObj->m_cf)
  113.                 {
  114.                 pSTM->tymed=TYMED_HGLOBAL;
  115.                 return pObj->RenderNative(&pSTM->hGlobal);
  116.                 }
  117.  
  118.             break;
  119.         }
  120.  
  121.     return ResultFromScode(DATA_E_FORMATETC);
  122.     }
  123.  
  124.  
  125.  
  126.  
  127. /*
  128.  * CImpIDataObject::GetDataHere
  129.  *
  130.  * Purpose:
  131.  *  Renders the specific FormatEtc into caller-allocated medium
  132.  *  provided in pSTM.
  133.  *
  134.  * Parameters:
  135.  *  pFE             LPFORMATETC describing the desired data.
  136.  *  pSTM            LPSTGMEDIUM providing the medium into which
  137.  *                  wer render the data.
  138.  *
  139.  * Return Value:
  140.  *  HRESULT         NOERROR on success, error code otherwise.
  141.  */
  142.  
  143. STDMETHODIMP CImpIDataObject::GetDataHere(LPFORMATETC pFE, LPSTGMEDIUM pSTM)
  144.     {
  145.     //No support for this function.
  146.     return ResultFromScode(DATA_E_FORMATETC);
  147.     }
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154. /*
  155.  * CImpIDataObject::QueryGetData
  156.  *
  157.  * Purpose:
  158.  *  Tests if a call to ::GetData with this FormatEtc will provide
  159.  *  any rendering; used like IsClipboardFormatAvailable.
  160.  *
  161.  * Parameters:
  162.  *  pFE             LPFORMATETC describing the desired data.
  163.  *
  164.  * Return Value:
  165.  *  HRESULT         NOERROR on success, error code otherwise.
  166.  */
  167.  
  168. STDMETHODIMP CImpIDataObject::QueryGetData(LPFORMATETC pFE)
  169.     {
  170.     LPCPolyline     pObj=(LPCPolyline)m_pObj;
  171.     UINT            cf=pFE->cfFormat;
  172.     BOOL            fRet=FALSE;
  173.  
  174.     /*
  175.      * This function is just cycling through each format you support
  176.      * and finding a match with the requested one, returning NOERROR
  177.      * if you do have it, S_FALSE otherwise.
  178.      */
  179.  
  180.     //Check the aspects we support.
  181.     if (!(DVASPECT_CONTENT & pFE->dwAspect))
  182.         return ResultFromScode(S_FALSE);
  183.  
  184.     switch (cf)
  185.         {
  186.         case CF_METAFILEPICT:
  187.             fRet=(BOOL)(pFE->tymed & TYMED_MFPICT);
  188.             break;
  189.  
  190.         case CF_BITMAP:
  191.             fRet=(BOOL)(pFE->tymed & TYMED_GDI);
  192.             break;
  193.  
  194.         default:
  195.             //Check our own format.
  196.             fRet=((cf==pObj->m_cf) && (BOOL)(pFE->tymed & TYMED_HGLOBAL));
  197.             break;
  198.         }
  199.  
  200.     return fRet ? NOERROR : ResultFromScode(S_FALSE);
  201.     }
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208. /*
  209.  * CImpIDataObject::GetCanonicalFormatEtc
  210.  *
  211.  * Purpose:
  212.  *  Provides the caller with an equivalent FormatEtc to the one
  213.  *  provided when different FormatEtcs will produce exactly the
  214.  *  same renderings.
  215.  *
  216.  * Parameters:
  217.  *  pFEIn            LPFORMATETC of the first description.
  218.  *  pFEOut           LPFORMATETC of the equal description.
  219.  *
  220.  * Return Value:
  221.  *  HRESULT         NOERROR on success, error code otherwise.
  222.  */
  223.  
  224. STDMETHODIMP CImpIDataObject::GetCanonicalFormatEtc(LPFORMATETC pFEIn
  225.     , LPFORMATETC pFEOut)
  226.     {
  227.     /*
  228.      *  1.  If you support an equivalent of pFEIn, return it in pFEOut.
  229.      *  2.  Return NOERROR if you filled pFEOut with anything, otherwise
  230.      *      return DATA_S_SAMEFORMATETC.  If you say that all renderings
  231.      *      are identical, return DATA_S_SAMEFORMATETC.
  232.      */
  233.  
  234.     return ResultFromScode(DATA_S_SAMEFORMATETC);
  235.     }
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242. /*
  243.  * CImpIDataObject::SetData
  244.  *
  245.  * Purpose:
  246.  *  Places data described by a FormatEtc and living in a StgMedium
  247.  *  into the object.  The object may be responsible to clean up the
  248.  *  StgMedium before exiting.
  249.  *
  250.  * Parameters:
  251.  *  pFE             LPFORMATETC describing the data to set.
  252.  *  pSTM            LPSTGMEDIUM containing the data.
  253.  *  fRelease        BOOL indicating if this function is responsible for
  254.  *                  freeing the data.
  255.  *
  256.  * Return Value:
  257.  *  HRESULT         NOERROR on success, error code otherwise.
  258.  */
  259.  
  260. STDMETHODIMP CImpIDataObject::SetData(LPFORMATETC pFE, STGMEDIUM FAR *pSTM
  261.     , BOOL fRelease)
  262.     {
  263.     LPCPolyline     pObj=(LPCPolyline)m_pObj;
  264.     UINT            cf=pFE->cfFormat;
  265.     BOOL            fRet=FALSE;
  266.     LPPOLYLINEDATA  ppl;
  267.  
  268.     //Check for our own clipboard format and DVASPECT_CONTENT
  269.     if ((cf!=pObj->m_cf) || !(DVASPECT_CONTENT & pFE->dwAspect))
  270.         return ResultFromScode(DATA_E_FORMATETC);
  271.  
  272.     /*
  273.      * Data can only come from global memory containing a POLYLINEDATA
  274.      * structure that we send to the Polyline's DataSet, a now internal
  275.      * function used from here and from IPersistStorage::Load.
  276.      */
  277.  
  278.     if (TYMED_HGLOBAL!=pSTM->tymed)
  279.         return ResultFromScode(DATA_E_FORMATETC);
  280.  
  281.     ppl=(LPPOLYLINEDATA)GlobalLock(pSTM->hGlobal);
  282.  
  283.     if (NULL!=ppl)
  284.         {
  285.         pObj->DataSet(ppl, TRUE, TRUE);
  286.         GlobalUnlock(pSTM->hGlobal);
  287.         fRet=TRUE;
  288.         }
  289.  
  290.     if (fRelease)
  291.         ReleaseStgMedium(pSTM);
  292.  
  293.     return fRet ? NOERROR : ResultFromScode(DATA_E_FORMATETC);
  294.     }
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301. /*
  302.  * CImpIDataObject::EnumFormatEtc
  303.  *
  304.  * Purpose:
  305.  *  Returns an IEnumFORMATETC object through which the caller can iterate
  306.  *  to learn about all the data formats this object can provide through
  307.  *  either ::GetData[Here] or ::SetData.
  308.  *
  309.  * Parameters:
  310.  *  dwDir           DWORD describing a data direction, either DATADIR_SET
  311.  *                  or DATADIR_GET.
  312.  *  ppEnum          LPENUMFORMATETC FAR * in which to return the pointer
  313.  *                  to the enumerator.
  314.  *
  315.  * Return Value:
  316.  *  HRESULT         NOERROR on success, error code otherwise.
  317.  */
  318.  
  319. STDMETHODIMP CImpIDataObject::EnumFormatEtc(DWORD dwDir
  320.     , LPENUMFORMATETC FAR *ppEnum)
  321.     {
  322.     LPCPolyline     pObj=(LPCPolyline)m_pObj;
  323.  
  324.     /*
  325.      * Create enumerators passing appropriate array pointers.
  326.      *
  327.      * The m_punkOuter passed to the enumerator is only used fo